package calao.client;

import java.util.Comparator;

import calao.client.i18n.Text;
import calao.client.ui.Format;
import calao.client.ui.HasLinks;
import calao.client.ui.HasTitle;
import calao.client.ui.Link;
import calao.client.ui.ListText;
import calao.client.ui.SortableGrid;
import calao.client.util.IntMap;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

class UserAdminPanel 
extends Composite
implements HasLinks, HasTitle
{

	class MemberTable extends SortableGrid 
	{ 
		MemberTable()
		{
			super(2);
			setHeader(0, text.user());
			setHeader(1, text.profiles());
			setColumnFormat(0, new Format() {
				public Widget formatAsWidget(Object data) {
					final MemberInfo minfo = (MemberInfo)data;
					return new Link(minfo.getName()) {
						protected void onClick() {
							Desktop.getDesktop().stackView(
									new MemberPanel(UserAdminPanel.this, minfo));
						}
					};
				}
			});
			setColumnComparator(0, new Comparator() {
				public int compare(Object obj1, Object obj2) {
					return ((MemberInfo)obj1).name.compareTo(((MemberInfo)obj2).name);
				}	
			});
		}
		protected Object getData(int row, int col) 
		{
			final MemberInfo minfo = memberInfo[row];
			switch (col) {
			case 0:
				return minfo;
			case 1:
				return getProfileListText(minfo.profiles);
			}
			return null;
		}
		String getProfileListText(int[] profiles)
		{
			ListText text = new ListText();
			for (int i=0; i<profiles.length; i++) {
				ProfileInfo pinfo = (ProfileInfo)profileMap.get(profiles[i]);
				text.add(pinfo!=null? pinfo.name : null);
			}
			return text.toString();
		}
		protected int getRowCount() 
		{
			return memberInfo!=null? memberInfo.length : 0;
		}
	}

	final static Text text = Desktop.getDesktop().text; 
	final DesktopPanel desktopPanel;
	final MemberTable table = new MemberTable();
	final IntMap profileMap = new IntMap();
	MemberInfo[] memberInfo;
	ProfileInfo[] profileInfo;

	UserAdminPanel(DesktopPanel desktopPanel)
	{
		this.desktopPanel = desktopPanel;
		initWidget(table);
		refresh();
	}
	
	private void set(ProfileInfo[] profileInfo)
	{
		this.profileInfo = profileInfo;
		for (int i=0; i<profileInfo.length; i++) {
			ProfileInfo info = profileInfo[i];
			profileMap.put(info.getId(), info);
		}
	}

	public Link[] getLinks()
	{
		return new Link[] { 
				new Link(text.newUser()) {
					protected void onClick() {
						Desktop.getDesktop().stackView(new MemberWizard(UserAdminPanel.this));
					}
				},
				new Link(text.profiles()) {
					protected void onClick() {
						Desktop.getDesktop().stackView(new ProfileTable());
					}
				}				
		};
	}

	public String getTitle() 
	{
		return text.users();
	}
	
	void refresh()
	{
		Desktop.getDesktop().memberService.listMembers(new ServiceCallback() {
			protected void onReturn(Object result) {
				memberInfo = (MemberInfo[])result;
			}
		});
		Desktop.getDesktop().memberService.listProfiles(new ServiceCallback() {
			protected void onReturn(Object result) {
				set((ProfileInfo[])result);
			}
		});
		ServiceCallback.defer(new Command() {
			public void execute() {
				table.refresh();
			}
		});		
	}

}
